﻿Imports System.Net.Http
Imports System.Net.Http.Headers

Public Class HomeController
    Inherits System.Web.Mvc.Controller
    ''' <summary>
    ''' constant username for CACTUS Web Service
    ''' </summary>
    ''' <remarks>assigned by USOE</remarks>
    Private Const MyUserName As String = "Put Your Username Here"
    ''' <summary>
    ''' constant password for CACTUS Web Service
    ''' </summary>
    ''' <remarks>assigned by USOE</remarks>
    Private Const MyPassword As String = "Put Your Password Here"

    Function PingCACTUS() As ActionResult
        Dim resultFromSystem As DateTime
        resultFromSystem = DateTime.Now
        ViewBag.ResultFromSystem = resultFromSystem
        Dim HttpClient = New HttpClient
        HttpClient.BaseAddress = New System.Uri("https://cactus.schools.utah.gov/")
        HttpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

        Dim responseMessage = HttpClient.GetAsync("api/CACTUS/ping_no_log").Result()
        Dim PingTime = responseMessage.Content.ReadAsAsync(Of DateTime)().Result
        ViewBag.ResultFromCACTUS = PingTime
        Return View()
    End Function

    Private Function GetAssignments() As IEnumerable(Of AssignmentDump)
        Dim HttpClient = New HttpClient
        HttpClient.BaseAddress = New System.Uri("https://cactus.schools.utah.gov/")
        HttpClient.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

        Dim responseMessage = HttpClient.GetAsync("api/CACTUS/CactusAssignmentDump?username=" + MyUserName + "&password=" + MyPassword).Result()
        Dim assignments = responseMessage.Content.ReadAsAsync(Of IEnumerable(Of AssignmentDump))().Result
        Return assignments
    End Function

    Function AssignmentDump() As ActionResult
        Return View(GetAssignments())
    End Function

#Region "Template Stuff"
    Function Index() As ActionResult
        ViewData("Message") = "Modify this template to jump-start your ASP.NET MVC application."

        Return View()
    End Function

    Function About() As ActionResult
        ViewData("Message") = "Your app description page."

        Return View()
    End Function

    Function Contact() As ActionResult
        ViewData("Message") = "Your contact page."

        Return View()
    End Function
#End Region
End Class
